package zelix.hack.hacks;

import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemSword;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import org.lwjgl.input.Mouse;
import zelix.hack.Hack;
import zelix.hack.HackCategory;
import zelix.utils.*;
import zelix.value.BooleanValue;
import zelix.value.Mode;
import zelix.value.ModeValue;
import net.minecraft.network.play.client.CPacketPlayerDigging;
import net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.client.event.InputUpdateEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.common.gameevent.TickEvent.PlayerTickEvent;

import static zelix.hack.hacks.AimAssist.mc;

public class NoSlow extends Hack {
	TimerUtils timer = new TimerUtils();
	ModeValue mode;
	ModeValue action;
	BooleanValue sendpacket;

	public NoSlow() {
		super("NoSlow", HackCategory.MOVEMENT);
		sendpacket = new BooleanValue("SendPacket", true);
		this.mode = new ModeValue("Mode", new Mode("AAC1", false), new Mode("AAC2", true), new Mode("HytPit" , false), new Mode("HYT 4V4" , false));
		this.action = new ModeValue("ActionMode", new Mode("ABORT", false), new Mode("START", true),
				new Mode("STOP", false),new Mode("RELEASE", false));
		this.addValue(sendpacket, mode, action);
	}

	@Override
	public void onInputUpdate(InputUpdateEvent event) {
		Utils.nullCheck();
		if (Wrapper.INSTANCE.player().isHandActive() && !Wrapper.INSTANCE.player().isRiding()) {
			event.getMovementInput().moveStrafe *= 5;
			event.getMovementInput().moveForward *= 5;
		}
		if(mode.getMode("HYT 4V4").isToggled()) {
			if (Mouse.isButtonDown(1) && mc.player.getHeldItem(EnumHand.MAIN_HAND).getItem() instanceof ItemSword) {
				mc.getConnection().sendPacket(new CPacketPlayerTryUseItemOnBlock(new BlockPos(-1, -1, -1), Minecraft.getMinecraft().objectMouseOver.sideHit,EnumHand.MAIN_HAND, 0f, 0f, 0f));
			}
		}

	}

	@Override
	public void onPlayerTick(PlayerTickEvent event) {
		if (!sendpacket.getValue()) {
			return;
		}
		if (mode.getMode("AAC1").isToggled()) {
			if (Wrapper.INSTANCE.player().isHandActive() && !Wrapper.INSTANCE.player().isRiding()
					&& MoveUtils.isMoving()) {
				if (event.phase == Phase.START) {
					if (Wrapper.INSTANCE.player().onGround || MoveUtils.isOnGround(0.5)) {
						Wrapper.INSTANCE.sendPacket(new CPacketPlayerDigging(
								CPacketPlayerDigging.Action.RELEASE_USE_ITEM, new BlockPos(0, 0, 0), EnumFacing.DOWN));
					}
				} else if (event.phase == Phase.END) {
					if (timer.delay(65)) {

						Wrapper.INSTANCE.sendPacket(new CPacketPlayerTryUseItemOnBlock(new BlockPos(-1, -1, -1),
								EnumFacing.DOWN, EnumHand.MAIN_HAND, 0.0F, 0.0F, 0.0F));
						timer.reset();
					}
				}
			}
		} else if (mode.getMode("AAC2").isToggled()) {
			if (Wrapper.INSTANCE.player().isHandActive() && !Wrapper.INSTANCE.player().isRiding()
					&& MoveUtils.isMoving()) {
				if (event.phase == Phase.START) {
					if (action.getMode("ABORT").isToggled()) {

						Wrapper.INSTANCE
								.sendPacket(new CPacketPlayerDigging(CPacketPlayerDigging.Action.ABORT_DESTROY_BLOCK,
										new BlockPos(0, 0, 0), EnumFacing.DOWN));
					} else if (action.getMode("START").isToggled()) {

						Wrapper.INSTANCE
								.sendPacket(new CPacketPlayerDigging(CPacketPlayerDigging.Action.START_DESTROY_BLOCK,
										new BlockPos(0, 0, 0), EnumFacing.DOWN));
					} else if (action.getMode("STOP").isToggled()) {
						Wrapper.INSTANCE
								.sendPacket(new CPacketPlayerDigging(CPacketPlayerDigging.Action.STOP_DESTROY_BLOCK,
										new BlockPos(0, 0, 0), EnumFacing.DOWN));
					}else if(action.getMode("RELEASE").isToggled()) {
						Wrapper.INSTANCE
						.sendPacket(new CPacketPlayerDigging(CPacketPlayerDigging.Action.RELEASE_USE_ITEM,
								new BlockPos(0, 0, 0), EnumFacing.DOWN));
					}
				} else if (event.phase == Phase.END) {
					Wrapper.INSTANCE.sendPacket(new CPacketPlayerTryUseItemOnBlock(new BlockPos(-1, -1, -1),
							EnumFacing.DOWN, EnumHand.MAIN_HAND, 0.0F, 0.0F, 0.0F));
				}

			}
		} else if (mode.getMode("HytPit").isToggled()) {

		}
		super.onPlayerTick(event);
	}

}
